package com.jackrain.nea.ad.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

@Mapper
public interface AdactionMapper {
    /**
     * 查看AD_ACTION表是否存在id的记录
     * @param id
     * @return
     */
    @Select("SELECT COUNT(*) FROM AD_ACTION WHERE ID = #{id}")
    int findCountById(Long id);

    /**
     * 根据id获取isactive字段
     * @param id
     * @return
     */
    @Select("SELECT isactive FROM AD_ACTION WHERE ID = #{id}")
    String findIsactiveById(Long id);

    class ActionSqlProvider {
        public String updateSql(JSONObject map) {
            Long id = map.getLong("ID");
            map.remove("ID");
            return new SQL() {
                {
                    UPDATE("AD_ACTION");
                    for (String key : map.keySet()) {
                        SET(key + "= #{" + key + "}");
                    }
                    WHERE("ID = " + id);
                }
            }.toString();
        }

        public String insertSql(JSONObject map){
            return new SQL(){
                {
                    INSERT_INTO("AD_ACTION");
                    for (String key : map.keySet()){
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }
    }

    /**
     * 更新AD_ACTION表
     * @param map
     * @return
     */
    @UpdateProvider(type = ActionSqlProvider.class, method = "updateSql")
    int update(JSONObject map);

    /**
     * 新增AD_ACTION表
     * @param map
     * @return
     */
    @InsertProvider(type = ActionSqlProvider.class, method = "insertSql")
    int insert(JSONObject map);

    @Delete("DELETE FROM AD_ACTION WHERE ID = #{id}")
    int delete(Long id);
}
